package tv.accedo.memory.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import tv.accedo.memory.model.Achi;
import tv.accedo.memory.model.User;
import tv.accedo.memory.util.HibernateUtil;

public class UserDao {

	public boolean addAchiToUser(String achi_sn, String username) {
		User user = new User(); 
		Achi achi = new Achi(); 
		Set<Achi> achis = new HashSet<Achi>(); 
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("from Achi achi where achi.achi_sn = ?")
				.setParameter(0, achi_sn); 
		achi = (Achi) query.uniqueResult(); 
//		System.out.println("ACHIEVEMENT NAME:" + achi.getAchi_name());
		int achi_id = achi.getId(); 
		
		query = session.createQuery("from User user where user.username = ?")
				.setParameter(0, username); 
		user = (User) query.uniqueResult(); 
		int user_id = user.getId(); 
		
		if (user.getAchis().contains(achi)) {
			session.createSQLQuery("update t_user_achi set achi_num=achi_num+1 where user_id = ? and achi_id = ?")
				   .setParameter(0, user_id)
			       .setParameter(1, achi_id)
			       .executeUpdate();
		} else {
			user.getAchis().add(achi); 
			session.save(user); 
			
//			session.createSQLQuery("update t_user_achi set achi_num=1, user_id = ?, achi_id = ?")
//			       .setParameter(0, user_id)
//		           .setParameter(1, achi_id)
//		           .executeUpdate();
		}
//		System.out.println("USER NAME:" + user.getUsername());
//		achis = user.getAchis(); 
//		
//		query = session.create
		
//		achis.add(achi); 
//		user.setAchis(achis);
//		session.save(achi); 
//		session.save(user); 
		
		session.getTransaction().commit();
//		session.close(); 
		return true; 
	}

	public long getScoreByUsername(String username) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = session.createQuery(
				"select user.score from User user where user.username = ?")
				.setParameter(0, username);
		Long score = (Long) query.uniqueResult();
		session.flush();
		session.close();
		return score;
	}

	public void addScoreToUser(int score, String username) {
		User user = new User();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = session.createQuery(
				"from User user where user.username = ?").setParameter(0,
				username);
		user = (User) query.uniqueResult();

		Long score_old = user.getScore();
		Long score_new = score_old + score;
		user.setScore(score_new);

		session.save(user);
		session.getTransaction().commit();
	}
	
	public User getUserByUsername(String username) {
		User user = new User();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = session.createQuery(
				"from User user where user.username = ?").setParameter(0,
				username);
		user = (User) query.uniqueResult();
		session.flush();
		session.close();
		return user; 
	}

	public int getCardNumByUserIdandAchiId(int user_id, int achi_id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = session.createSQLQuery("select achi_num from t_user_achi where user_id = ? and achi_id = ?")
		   .setParameter(0, user_id)
	       .setParameter(1, achi_id);
	    int card_num = (Integer) query.uniqueResult();    
		session.flush();
		session.close();
		return card_num; 
	}
	
	public List<User> getAllUsers() {
		List<User> userList = new ArrayList<User>();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Query query = session
				.createQuery("from User user order by user.score desc");
		userList = query.list();
		session.flush();
		session.close();
		return userList;
	}
}
